#include<stdio.h>
#include<Matrix/LB_Matrix.h>
#include<Matrix/LB_MatrixOperator.h>
#include<Algorithm/subdivision_of_polygon.h>
#include<Algorithm/convex_opt.h>


// v1 820 -0.100211160 0.876258400 0.381648800 v2 7627 -0.103921473 0.877581107 0.383118201 v2 7632 -0.103775028 0.877729105 0.383115516
// v2 7631 -0.102112259 0.878682546 0.382884186 v2 7630 -0.100007818 0.879889243 0.382591409 v2 7628 -0.099084489 0.880122578 0.382381145
// v2 7629 -0.096887376 0.880677812 0.381880811 v2 7633 -0.096076444 0.880852586 0.381687812 v2 7634 -0.095691479 0.880935555 0.381596191

void test_combination()
{
    int* c=NULL;
    int len=0;
    int m=3,n=7;
    c=compute_combination(m,n,c,&len);
    printf("len:%d\n",len);
    for(int i=0;i<len;i++)
    {
        for(int j=0;j<m;j++)
        {
            printf("%d ",c[i*3+j]);
        }
        printf("\n");
    }
}



//
void test_convex1()
{
    double l[6]={0.36,1.34,0.491,0,0,0};
    normalize(l,3);

    printf("l:%lf %lf %lf\n",l[0],l[1],l[2]);
    double p[3]={-0.1,-2.3,8.9};

    double t=point_nearest_2_line(p,l);
    double point[3]={ t*l[0]+l[3],t*l[1]+l[4],t*l[2]+l[5]};
    printf("rsult t:%lf point:%lf %lf %lf\n",t,point[0],point[1],point[2]);

    printf("dis:%lf\n",distance_of_two_points( p, point,3));
    double dir1[3]={point[0]-p[0],point[1]-p[1],point[2]-p[2] };
    printf("delta:%lf\n",inner_product(dir1,l,3));
    printf("end test convec1\n");


    double p1[3]={l[3],l[4],l[5]};
    double p2[3]={l[0]+l[3],l[1]+l[4],l[2]+l[5]};

    double **H=(double**)malloc(sizeof(double*)*2);
    for(int i=0;i<2;i++)
    {
        H[i]=(double*)malloc(sizeof(double)*2);
    }
    H[0][0]=inner_product(p1,p1,3)*2; H[0][1]=inner_product(p1,p2,3)*2;
    H[1][0]=inner_product(p1,p2,3)*2; H[1][1]=inner_product(p2,p2,3)*2;

    double* C=(double*)malloc(sizeof(double)*2);
    C[0]=-2*inner_product(p,p1,3);C[1]=-2*inner_product(p,p2,3);


    double**A=NULL;double*b=NULL;
    double**E=(double**)malloc(sizeof(double*));
    E[0]=(double*)malloc(sizeof(double)*2);
    E[0][0]=1;E[0][1]=1;
    double *d=(double*)malloc(sizeof(double));
    d[0]=1;

    double * x=(double*)malloc(sizeof(double)*2);
    memset(x,0,sizeof(double)*2);

    opt_quadratic_problem_with_constraint(H,C,2 ,
        A,  b ,0,
        E,  d,1,x);



// double gradient_descent_for_solving_systems_of_equations(
//     double **A,double*b,double*x,
//     int row,int col,double delta);


}

int main()
{
    test_convex1();
    test_combination();
    int i=0;
    for(;i<5;i++)
    {
        if(i==2){break;}
    }
    printf("i:%d\n",i);
    double ta=1.9;
    printf(" %d\n",(int)ta);
    Matrix c;
    Matrix a(2,2);
    Matrix b(a);
    *((double*)a.element(0,0))=1.0;*((double*)a.element(0,1))=2.0;
    *((double*)a.element(1,0))=1.0;*((double*)a.element(1,1))=2.0;
    c=a*b;

    double **vv=(double**)malloc(sizeof(double*)*11);
    for(int i=0;i<11;i++)
    {
        vv[i]=(double*)malloc(sizeof(double)*3);
    }

    vv[0][0]=-0.105942520;vv[0][1]=0.878301600;vv[0][2]=0.383918600;
    vv[1][0]=-0.094833140;vv[1][1]=0.881823800;vv[1][2]=0.381586200;
    vv[2][0]=-0.095690385;vv[2][1]=0.880935781;vv[2][2]=0.381595971;

   //v1 90308 0.308995 -8.500475 0.000000 v2 1844 0.368237 -8.454475 0.000000 v2 1845 0.364908 -8.448628 0.000000
    vv[3][0]=-0.095691479;vv[3][1]=0.880935555;vv[3][2]=0.381596191;
    vv[4][0]=-0.096076444;vv[4][1]=0.880852586;vv[4][2]=0.381687812;
    vv[5][0]=-0.096887376;vv[5][1]=0.880677812;vv[5][2]=0.381880811;

    vv[6][0]=-0.099084489;vv[6][1]=0.880122578;vv[6][2]=0.382381145;
    vv[7][0]=-0.100007818;vv[7][1]=0.879889243;vv[7][2]=0.382591409;
    vv[8][0]=-0.102112259;vv[8][1]=0.878682546;vv[8][2]=0.382884186;

    vv[9][0]=-0.103775028;vv[9][1]=0.877729105;vv[9][2]=0.383115516;
    vv[10][0]=-0.103921473;vv[10][1]=0.877581107;vv[10][2]=0.383118201;


    int** s=( int**)malloc(sizeof(int*)*9);
    for(int i=0;i<9;i++)
    {
        s[i]=(int*)malloc(sizeof(int)*3);
        memset(s[i],0,sizeof(int)*3);
    }
    subdivision_of_polygon(vv,11 ,s);

    for(int i=0;i<9;i++)
    {
        printf("%d %d %d\n",s[i][0],s[i][1],s[i][2]);
    }

    //v2 1846 0.363907 -8.446858 0.000000 v2 1847 0.363665 -8.446430 0.000000 v1 90307 0.305619 -8.446335 0.000000

    // double testn[3]={1e-14,0,0};
    // double testn1[3]={0};
    // double dis=distance_of_two_points(testn,testn1,3);
    // printf("dis:%.16lf\n",dis);

    printf("endddd\n");
    return 0;
}
